home *** CD-ROM | disk | FTP | other *** search
/ Delphi 2.0 - Programmer's Utilities Power Pack / Delphi 2.0 Programmer's Utilities Power Pack.iso / a_to_d / delftips / ti2855.asc < prev    next >
Encoding:
Text File  |  1996-09-15  |  2.5 KB  |  122 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.   PRODUCT  :  Delphi                                 NUMBER  :  2855
  8.   VERSION  :  All
  9.        OS  :  Windows
  10.      DATE  :  August 22, 1995                          PAGE  :  1/2
  11.  
  12.     TITLE  :  How to copy files in Delphi.
  13.  
  14.  
  15.  
  16.  
  17. Q:  How do I copy a file?
  18.  
  19. A:  Here are three ways:
  20.  
  21. {This way uses a File stream.}
  22. Procedure FileCopy( Const sourcefilename, targetfilename: String );
  23. Var
  24.   S, T: TFileStream;
  25. Begin
  26.   S := TFileStream.Create( sourcefilename, fmOpenRead );
  27.   try
  28.     T := TFileStream.Create( targetfilename,
  29.                              fmOpenWrite or fmCreate );
  30.     try
  31.       T.CopyFrom(S, S.Size ) ;
  32.     finally
  33.       T.Free;
  34.     end;
  35.   finally
  36.     S.Free;
  37.   end;
  38. End;
  39.  
  40. {This way uses memory blocks for read/write.}
  41. procedure FileCopy(const FromFile, ToFile: string);
  42.  var
  43.   FromF, ToF: file;
  44.   NumRead, NumWritten: Word;
  45.   Buf: array[1..2048] of Char;
  46. begin
  47.   AssignFile(FromF, FromFile);
  48.   Reset(FromF, 1);        { Record size = 1 }
  49.   AssignFile(ToF, ToFile);    { Open output file }
  50.   Rewrite(ToF, 1);        { Record size = 1 }
  51.   repeat
  52.     BlockRead(FromF, Buf, SizeOf(Buf), NumRead);
  53.     BlockWrite(ToF, Buf, NumRead, NumWritten);
  54.   until (NumRead = 0) or (NumWritten <> NumRead);
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.   PRODUCT  :  Delphi                                 NUMBER  :  2855
  69.   VERSION  :  All
  70.        OS  :  Windows
  71.      DATE  :  August 22, 1995                          PAGE  :  2/2
  72.  
  73.     TITLE  :  How to copy files in Delphi.
  74.  
  75.  
  76.  
  77.  
  78.   CloseFile(FromF);
  79.   CloseFile(ToF);
  80. end;
  81.  
  82. {This one uses LZCopy, which USES LZExpand.}
  83. procedure CopyFile(FromFileName, ToFileName: string);
  84. var
  85.   FromFile, ToFile: File;
  86. begin
  87.   AssignFile(FromFile, FromFileName); { Assign FromFile to FromFileName }
  88.   AssignFile(ToFile, ToFileName);     { Assign ToFile to ToFileName }
  89.   Reset(FromFile);                    { Open file for input }
  90.   try
  91.     Rewrite(ToFile);                  { Create file for output }
  92.     try
  93.       { copy the file an if a negative value is returned }
  94.       { raise an exception }
  95.       if LZCopy(TFileRec(FromFile).Handle, TFileRec(ToFile).Handle) < 0
  96.         then
  97.         raise EInOutError.Create('Error using LZCopy')
  98.     finally
  99.       CloseFile(ToFile);  { Close ToFile }
  100.     end;
  101.   finally
  102.     CloseFile(FromFile);  { Close FromFile }
  103.   end;
  104. end;
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118. DISCLAIMER: You have the right to use this technical information
  119. subject to the terms of the No-Nonsense License Statement that
  120. you received with the Borland product to which this information
  121. pertains.
  122.